package com.example.app.ui.Messages;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.example.app.R;
import com.example.app.databinding.FragmentMessagesBinding;

import java.util.ArrayList;
import java.util.List;

public class MessagesFragment extends Fragment {
    private FragmentMessagesBinding binding;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
//        new ViewModelProvider(requireActivity()).get();
    }
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentMessagesBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        ActionBar supportActionBar = activity.getSupportActionBar();
        if (supportActionBar != null && supportActionBar.getCustomView() != null) {
            View customView = supportActionBar.getCustomView();
            TextView textViewTitle = customView.findViewById(R.id.textView_title);
            if (textViewTitle != null) {
                textViewTitle.setText("消息");
            }
        }
        ListView listView = root.findViewById(R.id.messagelv);
        for (int i = 0; i < 20; i++) {
        }
        SharedPreferences sharedPreferences = requireContext().getSharedPreferences("user_credentials", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", null);
        String password = sharedPreferences.getString("password", null);
        System.out.println("Home   \n  "+username+"   \n"+password);
        List<Message> messages = generateMockData();
        MessagesAdapter adapter = new MessagesAdapter(requireContext(), messages);
        listView.setAdapter(adapter);

        return root;
    }
    private List<Message> generateMockData() {
        List<Message> messages = new ArrayList<>();
        // 添加模拟数据
        messages.add(new Message("张三", "你好呀！", "10:30", 2, R.drawable.tx));
        messages.add(new Message("李四", "[图片]", "09:45", 0, R.drawable.xm));
        messages.add(new Message("王五", "晚上一起吃饭吗？", "昨天", 5, R.drawable.tx1));
        messages.add(new Message("赵六", "你好呀！", "10:30", 2, R.drawable.tx2));
        messages.add(new Message("小李", "[图片]", "09:45", 1, R.drawable.tx4));
        messages.add(new Message("小绿", "[图片]", "09:45", 2, R.drawable.tx5));
        messages.add(new Message("小驴", "[图片]", "09:45", 3, R.drawable.tx6));
        messages.add(new Message("小马", "[图片]", "09:45", 4, R.drawable.tx7));
        messages.add(new Message("骆驼", "[图片]", "09:45", 5, R.drawable.tx8));
        messages.add(new Message("小妹", "[图片]", "09:45", 6, R.drawable.tx9));
        messages.add(new Message("大美", "[图片]", "09:45", 7, R.drawable.tx10));
        messages.add(new Message("小美", "[图片]", "09:45", 8, R.drawable.tx11));
        messages.add(new Message("小红", "[图片]", "09:45", 9, R.drawable.tx12));
        messages.add(new Message("小黄", "[图片]", "09:45", 10, R.drawable.tx13));
        messages.add(new Message("小蓝", "[图片]", "09:45", 11, R.drawable.tx14));
        messages.add(new Message("李四", "[图片]", "09:45", 12, R.drawable.tx15));
        messages.add(new Message("李四", "[图片]", "09:45", 13, R.drawable.tx16));
        messages.add(new Message("李四", "[图片]", "09:45", 14, R.drawable.tx17));
        messages.add(new Message("李四", "[图片]", "09:45", 15, R.drawable.tx18));
        messages.add(new Message("李四", "[图片]", "09:45", 16, R.drawable.tx19));
        messages.add(new Message("李四", "[图片]", "09:45", 17, R.drawable.tx20));
        messages.add(new Message("李四", "[图片]", "09:45", 18, R.drawable.tx21));
        messages.add(new Message("李四", "[图片]", "09:45", 19, R.drawable.tx22));
        messages.add(new Message("李四", "[图片]", "09:45", 21, R.drawable.tx23));
        messages.add(new Message("李四", "[图片]", "09:45", 22, R.drawable.tx24));
        return messages;
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}